import { axiosRequest } from './axios'
import { getParamsStr } from '@tools'

export interface ApiResponse<T = null> {
    code: number | string;
    data: T;
    message: string;
    total: number;
}
export interface ApiError {
    code?: number;
    message?: string;
}


class AxiosService {
    public get<T>(url: string, data: { [key: string]: any } = {}, config?: AxiosType): Promise<ApiResponse<T>> {
        return new Promise((resolve, reject) => {
            const query = getParamsStr(data)
            axiosRequest.instance.get<ApiResponse<T>>(url + '?' + query, config)
                .then(axiosRes => {
                    if (axiosRes.data.code === 200) {
                        resolve(axiosRes.data)
                    } else {
                        reject(axiosRes.data)
                    }
                }).catch((error: ApiError) => {
                    reject(error)
                })
        })
    }

    public json<T>(url: string, data: { [key: string]: any } = {}, config?: AxiosType): Promise<ApiResponse<T>> {
        return new Promise((resolve, reject) => {
            axiosRequest.instance.post<ApiResponse<T>>(url + data, config)
                .then(axiosRes => {
                    if (axiosRes.data.code === 200) {
                        resolve(axiosRes.data)
                    } else {
                        reject(axiosRes.data)
                    }
                })
                .catch((error: ApiError) => {
                    reject(error)
                })
        })
    }

    public post<T>(url: string, data?: { [propName: string]: any } | string, config?: AxiosType): Promise<ApiResponse<T>> {
        return new Promise((resolve, reject) => {
            axiosRequest.instance.post<ApiResponse<T>>(url, data, config)
                .then(axiosRes => {
                    if (axiosRes.data.code === 200) {
                        resolve(axiosRes.data)
                    } else {
                        reject(axiosRes.data)
                    }
                })
                .catch((error: ApiError) => {
                    reject(error)
                })
        })
    }

    public put<T>(url: string, data: { [propName: string]: any } | null, config?: AxiosType): Promise<ApiResponse<T>> {
        return new Promise((resolve, reject) => {
            axiosRequest.instance.put<ApiResponse<T>>(url, data, config)
                .then(axiosRes => {
                    if (axiosRes.data.code === 200) {
                        resolve(axiosRes.data)
                    } else {
                        reject(axiosRes.data)
                    }
                })
                .catch((error: ApiError) => {
                    reject(error)
                })
        })
    }

    public delete(url: string, config: AxiosType): Promise<ApiResponse> {
        return new Promise((resolve, reject) => {
            axiosRequest.instance.delete<ApiResponse>(url, config)
                .then(axiosRes => resolve(axiosRes.data))
                .catch((error: ApiError) => {
                    reject(error)
                })
        })
    }
}


export const axiosService = new AxiosService()
